package cn.e4j.modular.system.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.model.exception.ServiceException;
import cn.e4j.modular.system.entity.SysCustoms;
import cn.e4j.modular.system.mapper.SysCustomsMapper;
import cn.e4j.modular.system.service.SysCustomsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 业务对象维护
 *
 * @author miaoyl
 * @Date 2018/01/04
 */
@Controller
@RequestMapping("/sys/businessObject")
public class SysCustomController extends BaseController {

    @Autowired
    private SysCustomsMapper sysCustomsMapper;

    @Autowired
    private SysCustomsService sysCustomsService;

    /**
     * 跳转业务对象维护页面
     */
    @RequiresPermissions("sys:businessObject:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/customs.html";
    }

    /**
     * 跳转到查看新增页面
     */
    @RequestMapping("/customs_add")
    public String addView() {
        return "/pc/system/customs_edit.html";
    }

    /**
     * 查询业务对象列表
     */
    @RequestMapping("/getCustomslist")
    @RequiresPermissions("sys:businessObject:init")
    @ResponseBody
    public ResponseData getCustomslist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysCustomsService.getCustomslist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询业务对象列表
     */
    @RequestMapping("/getCustomsEdit")
    @RequiresPermissions("sys:businessObject:init")
    @ResponseBody
    public ResponseData getCustomsEdit(@DtoParam DtoWapper dtoWapper) {
        //Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysCustomsService.getCustomsEdit(inDto);
        //return new PageReData(page, result);
        return ResponseData.success(result);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @RequiresPermissions("sys:businessObject:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        /*if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }*/
        Dto inDto = dtoWapper.getDto();
        sysCustomsService.add(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("sys:businessObject:init")
    @ResponseBody
    public ResponseData del(SysCustoms sysCustoms, BindingResult result) {
        sysCustomsMapper.delByCustomsObj(sysCustoms.getCUSTOM_OBJ());
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:businessObject:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        sysCustomsService.edit(inDto);
        return SUCCESS_TIP;
    }
}
